<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/14
 * Time: 15:00
 */
//获取消费的年份
function getFiveYears($num=5){
    $years = db('income')->field(" FROM_UNIXTIME(create_time,'%Y') as year")->group('year')->limit($num)->select()->toArray();
    /*$years = [];
    $now_year = date('Y');
    for ($i = 4; $i>=1; $i--){
        $years[] = (int)$now_year - $i;
    }
    $years[] = (int)$now_year;*/
    return $years;
}

/**
 * 获取12个月份
 */
function getMoths(){
    $moths = [];
    $now_moth = 12;
    for ($i = 1; $i<=$now_moth; $i++){
        $moths[$i]['moth'] = (int)$i;
    }
    return $moths;
}

/**
 * 获取用户每月的总收益
 * @param  user_id  用户ID
 * @param year 年份  根据年份查询
 */
function getRegisterMothTotalMoney($user_id, $year, $moth=0){
      $incomeModel = db('income');

    //1.获取用户等级升级情况
    $ranks = db('reg_rank')->alias('r')
        ->field('r.*, ra.bonus, ra.base_money, ra.cell')
        ->join('hstht_rank ra', 'r.rank_id = ra.id', 'LEFT')
        ->where('r.reg_id', $user_id)->order('r.create_time asc')->group('r.rank_id')->select();

    $where = array(
        'inco_reg_id' => $user_id,
    );
    $count = count($ranks);
    $arr = array();
    $time1 = '';
    //根据等级情况进行循环
    foreach ($ranks as $key=>$val){
        $f_moth = date('m',$val['create_time']);
        $f_year = date('Y',$val['create_time']);

        //查询某个月的收益总额
        if ($f_year == $year && $f_moth == $moth){
            $field = "sum(i.inco_money) + {$val['base_money']} as shouyi";
            $list = $incomeModel->field($field)->alias('i')->join('hstht_consume_logs c', 'i.consu_id = c.id')->where($where)->find();
            return $list;
        }else{
            $where['c.year'] = $year;

            //根据时间判断用哪个级别
            if (empty($time1)){
                $time1 = date('m',$val['create_time'])+1;
                $where['c.moth'] = array('lt', $time1);
            }elseif($key+1 == $count){
                $where['c.moth'] = array('egt', date('m',$val['create_time'])+1);
            }else{
                $where['c.moth'] = array('between', [$time1, (date('m',$val['create_time'])+1)]);
            }
            $field = "i.id, c.moth, c.year,sum(money) total_money, sum(i.inco_money) + {$val['base_money']} as shouyi";
            $list = $incomeModel->field($field)->alias('i')->join('hstht_consume_logs c', 'i.consu_id = c.id', 'LEFT')->where($where)->group('c.moth')->select()->toArray();
            if (empty($arr)){
                $arr = $list;
            }else{
                $arr = array_merge($arr, $list);
            }

            $time1 = $val['create_time'];
        }

    }
    return $arr;
}

/**
 * 获取某个用户的总收益
 * @param $register_id 收益用户的ID
 */
function getRegisterTotalConsume($register_id){
    $field = "(sum(inco_money) + r.base_money) m_total_money";
    $where = array('inco_reg_id'=>$register_id);
    $join = [
        ['hstht_consume_logs c', 'i.consu_id=c.id'],
        ['hstht_rank r', 'c.rank_id=r.id'],
    ];
    $list = db('income')->alias('i')
        ->field($field)
        ->join($join)
        ->where($where)
        ->group("FROM_UNIXTIME(i.create_time, '%Y-%m')")
        ->select()->toArray();
    $sum = 0;
    foreach($list as $item){
        $sum += (float) $item['m_total_money'];
    }
    return $sum;
}

function getUserName($userId){
    $user_login = db('user')->where('id', $userId)->value('user_login');
    return $user_login;
}

function getCompanyName($id){
    $name = db('company')->where('id', $id)->value('name');
    return $name;
}